package com.example.mylibrarybase.yumengmeng07.utils.linesRadioGroup;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.example.mylibrarybase.R;
import com.example.mylibrarybase.utils.LogUtils;

import java.util.List;

/**
 * Created by Administrator 于萌萌
 * 创建日期: 10:55 . 2016年08月30日
 * 描述:多列RadioButton
 * <p>
 * <p>
 * 备注:
 */
public class LinesRadioGroup {

    private static FeedbackOptionItem selectedOptionItem;
    private static int selectedRadioGroupIndex;
    private static LinearLayout llOptionsContainer;

    public interface CallBackItemNum{
        void callback(int pos, String title, int linespos);
    }
    /**
     *
     * 意见反馈类型的列表 如 闪退问题、卡顿问题、功能建议
     * 一个radioGroup代表一行，一行有radioCount个radioButton
     * @description
     * @author zhongwr
     * @params
     * @update 2016年1月9日 上午11:48:15
            */
    public static void updateOptionsView(Context context , List<FeedbackOptionItem> optionsList, LinearLayout llOptionsContainer , final CallBackItemNum callBackItemNum) {
        if (optionsList!=null) {
            LinesRadioGroup.llOptionsContainer = llOptionsContainer;
            LinesRadioGroup.llOptionsContainer.removeAllViews();
            int size = optionsList.size();
            final int radioCount = 2;
            int mod = size % radioCount;
            int radioGroupCount = 0 == mod ? size / radioCount : size / radioCount + 1;
            RadioGroup.LayoutParams groupParams = new RadioGroup.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
                    LinearLayout.LayoutParams.WRAP_CONTENT);
            RadioGroup.LayoutParams radioParams = new RadioGroup.LayoutParams(RadioGroup.LayoutParams.WRAP_CONTENT,
                    RadioGroup.LayoutParams.WRAP_CONTENT);
            RadioGroup radioGroup = null;
            for (int i = 0; i < radioGroupCount; i++) {
                if (i != radioGroupCount - 1) {
                    groupParams.setMargins(0, 0, 0, 24);
                }
                radioGroup = new RadioGroup(context);
                radioGroup.setLayoutParams(groupParams);
                radioGroup.setOrientation(RadioGroup.HORIZONTAL);
                radioGroup.setWeightSum(radioCount);
                radioParams.setMargins(10,10,10,10);
                int radioMaxeSize = (i + 1) * radioCount;
                for (int j = i * radioCount; j < radioMaxeSize && j < size; j++) {
                    FeedbackOptionItem optionItem = optionsList.get(j);
                    optionItem.radioGroupIndex = i;
                    RadioButton radio = new RadioButton(context);
                    radio.setLayoutParams(radioParams);
                    radio.setText(optionItem.title);
                    radio.setTag(optionItem);

                    radio.setButtonDrawable(new ColorDrawable(Color.TRANSPARENT));
                    radio.setBackgroundResource(R.drawable.feedback_radiobtn_style_selector_07);
                    // 设置图片与文本的间距，据说必须先setButtonDrawable，再调用
                    radio.setPadding(20, 20, 20, 20);
                    radioGroup.addView(radio);
                }

                radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
                    /** 只要状态发生改变（从选中到未选中或者从未选中到选中） */
                    @Override
                    public void onCheckedChanged(RadioGroup group, int checkedId) {
                        RadioButton radioButton = (RadioButton) group.findViewById(checkedId);
                        if (null != radioButton && radioButton.isChecked()) {// 选中的item才会执行这个
                            selectedOptionItem = (FeedbackOptionItem) radioButton.getTag();
                           //  首次选中不需要重置各个radioGroup的状态
                            if (null != selectedOptionItem && -1 != selectedRadioGroupIndex
                                    && selectedRadioGroupIndex != selectedOptionItem.radioGroupIndex) {
                                resetUnselectedRadioGroupStatus(selectedOptionItem.radioGroupIndex);
                            }
                            selectedRadioGroupIndex = selectedOptionItem.radioGroupIndex;

                            callBackItemNum.callback(Integer.parseInt(selectedOptionItem.tid),selectedOptionItem.title,selectedRadioGroupIndex);
                        }
                    }


                });
                llOptionsContainer.addView(radioGroup);
            }
        }
    }


    /***
     *
     * @description 恢复未选中的radioGroup状态
     * @author zhongwr
     * @params radioGroupIndex 被选中的radioGroup的下标
     * @update 2016年1月9日 下午2:44:06
     */
    private static void resetUnselectedRadioGroupStatus(int radioGroupIndex) {
        if (null != llOptionsContainer) {
            // int size = radioGroupList.size();
            int size = llOptionsContainer.getChildCount();
            //Logcat.dLog("size = " + size);
            LogUtils.i("size = " + size);
            for (int i = 0; i < size; i++) {
                if (i == radioGroupIndex) {
                    continue;
                }
                View view = llOptionsContainer.getChildAt(i);
                if (null != view && view instanceof RadioGroup) {
                    ((RadioGroup) view).clearCheck();
                }
                // RadioGroup radioGroup = radioGroupList.get(i);
                // radioGroup.clearCheck();
            }
        }
    }

    /***
     * 意见反馈：信息详情item
     *
     * @author "zhongwr"
     *
     */
    public static class FeedbackOptionItem {
        /** 类型id */
        public String tid;

        public String title;



        public FeedbackOptionItem(String tid, String title) {
            this.tid = tid;
            this.title = title;
        }

        /**本地赋值： 当前item所属的radioGroup的下标 */
        public int radioGroupIndex;
    }
}
